package com.changgou.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.config.TokenDecode;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wz
 * @company 公司
 * @create 2020-04-17 17:15
 */
@RestController
@RequestMapping("/seckillOrder")
public class SeckillOrderController {

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private SeckillOrderService seckillOrderService;

    @RequestMapping("/add")
    public Result add(@RequestParam("time") String time, @RequestParam("id") Long id) {
        //1.动态获取当前登录人
        String username = tokenDecode.getUserInfo().get("username");
        //2.基于业务进行秒杀
        Boolean result = seckillOrderService.add(id, time, username);

        //3.返回结果
        if (result) {
            //下单成功
            return new Result(true, StatusCode.OK,"下单成功");
        } else {
            //下单失败
            return new Result(false,StatusCode.OK,"下单失败");
        }
    }
}
